package cn.china.springboot.controller;




import entity.Student;
import exception.MyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
public class MyController {

    @RequestMapping("/hello")
    public String hello(HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        return "hello";
    }


    @RequestMapping("/my")
    public String my(HttpServletRequest req) throws MyException {
        req.setAttribute("name","jack");
        int a=2,b=0,c;
        if(b==0){
            throw  new MyException("分母为0");
        }else{
            c = a / b ;
        }
        return "my";



    }

    @RequestMapping("/my2")
    public String my2(HttpServletRequest req){
        System.out.println("我是Controller的信息");
        return "my";
    }



    @RequestMapping("/stu")
    public Student student(@RequestParam String name, @RequestParam Integer age ){//,@RequestParam String json
        System.out.println("Controller中接收的值："+name+age);
//        System.out.println(JSON.parse(json));


        Student student = new Student();
        student.setName("tom2");
        student.setAge(19);
        student.setGender("男");
//        after :拿不到return值，return前执行myaspect的内容

        return student;
        //afterrunning :能拿到值
    }



    @RequestMapping("/stulist")
    public List<Student> studentlist(){

        List<Student> list= new ArrayList<>();

        Student student = new Student();
        student.setName("tom2");
        student.setAge(19);
        student.setGender("男");
        Student student1= new Student("张三",29,"女");
        list.add(student);list.add(student1);
        return list;

    }

    @RequestMapping("/mv")
    public ModelAndView modelAndView(ModelAndView mv){

        mv.addObject("name","王尼玛");
        mv.addObject("age",18);
        mv.setViewName("myv");
        return mv;

    }



}
